package com.wk.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.wk.entity.CmfzGuru;
import com.wk.service.CmfzGuruService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("guru")
@Slf4j
public class GuruController {

    @Autowired
    private CmfzGuruService cmfzGuruService;

    @CrossOrigin
    @RequestMapping("findAll")
    public Map findAll(Integer page,Integer limit){
        return cmfzGuruService.findAll(page,limit);
    }

    @RequestMapping("uploadExcel")
    public Map uploadExcel(MultipartFile file) throws Exception {
        InputStream inputStream = file.getInputStream();

        ImportParams importParams = new ImportParams();

        importParams.setHeadRows(1);
        importParams.setTitleRows(1);

        List<CmfzGuru> cmfzGurus = ExcelImportUtil.importExcel(inputStream, CmfzGuru.class, importParams);

        for (CmfzGuru gurus : cmfzGurus) {
            String guruImage = gurus.getGuruImage();

            int g = guruImage.indexOf("pic");
            String substring = guruImage.substring(g, guruImage.length());
            log.info("图片路径"+substring);
            gurus.setGuruImage(substring);
        }

        log.info("要添加的所有上师数据");

        return cmfzGuruService.mutilAdd(cmfzGurus);

    }

    @RequestMapping("download")
    public void download(HttpServletResponse response)throws Exception{

        List<CmfzGuru> cmfzGurus = cmfzGuruService.selectList();

        for (CmfzGuru gurus : cmfzGurus) {
            gurus.setGuruImage("http://localhost:8001/cmfz/img/"+ gurus.getGuruImage());
        }


        ExportParams exportParams = new ExportParams("所有上师数据","gurus");
        try {
            Workbook sheets = ExcelExportUtil.exportExcel(exportParams, CmfzGuru.class, cmfzGurus);
           response.setHeader("Content-Disposition","attachment;fileName="+URLEncoder.encode("gurus.xls","utf-8"));
            //response.setHeader("Content-Disposition","attachment;fileName");
            response.setContentType("application/vnd.ms-excel");
            sheets.write(response.getOutputStream());
            log.info("下载成功");
        } catch (IOException e) {
            e.printStackTrace();
            log.error("下载失败");
        }
    }

}
